import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd';

declare var ui: any;
declare var form: any;
declare var base: any;

@Component({
  templateUrl: './show.info.component.html'
})
export class ShowInfoComponent implements OnInit {

  // 当前数据
  is: any;
  ds: any;
  fs: any;
  ps: any;
  // 菜单
  menu = null;
  // 字典
  airportDic: any = null;

  child: any = {};
  index = -1;

  airport: any;
  airportName: any;
  selectDate: any;

  constructor(
    public route: ActivatedRoute,
    private modalService: NzModalService) {
    this.menu = new ui.TabService('bpm', 's');
    this.menu.setTab('main');
    // 注册表单
    this.is = new form.FormSimpleService('temporaryair', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 99999
    });
    this.ds = new form.FormSimpleService('temporaryadministration', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 99999
    });
    this.fs = new form.FormSimpleService('temporaryfloor', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 99999
    });
    this.ps = new form.FormSimpleService('temporarypolice', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 99999
    });
    this.airportDic = new form.DictionaryService('zd_airport');
    this.airportDic.loadAll();
  }

  ngOnInit() {
    this.initData();

    // 浏览器关闭监听
    window.onbeforeunload = () => {
      // tslint:disable-next-line: deprecation
      const e: any = window.event;
      e.returnValue = ('请确认已保存，是否离开？');
      return e;
    };
  }

  initData() {
    this.route.queryParams.subscribe((res) => {
      if (res.id) {
        this.airport = res.id;
        this.selectDate = this.dateFormat('YYYY-mm-dd', new Date());
      }
      if (res.date !== 'undefined') {
        this.selectDate = res.date;
      } else {
        this.selectDate = this.dateFormat('YYYY-mm-dd', new Date());
      }
    });

    if (this.airport) {
      this.ds.queryObject.airport = this.airport;
      this.fs.queryObject.airport = this.airport;
      this.ps.queryObject.airport = this.airport;
      this.is.queryObject.airport = this.airport;
      this.ds.queryObject['createTime:>='] = this.selectDate;
      this.is.queryObject['createTime:>='] = this.selectDate;
      this.ps.queryObject['createTime:>='] = this.selectDate;
      this.fs.queryObject['createTime:>='] = this.selectDate;
      this.is.queryObject['createTime:<='] = this.selectDate;
      this.ds.queryObject['createTime:<='] = this.selectDate;
      this.fs.queryObject['createTime:<='] = this.selectDate;
      this.ps.queryObject['createTime:<='] = this.selectDate;
      this.is.loadData(true, (data) => {
        if (data.length > 0) {
          this.is.er = this.is.datas[0];
        } else {
          this.is.er = [];
        }
      });

      this.ds.loadData(true, (data) => {
        if (data.length > 0) {
          this.ds.er = this.ds.datas[0];
        } else {
          this.ds.er = [];
        }
      });
      this.fs.loadData(true, (data) => {
        if (data.length > 0) {
          this.fs.er = this.fs.datas[0];
        } else {
          this.fs.er = [];
        }
      });
      this.ps.loadData(true, (data) => {
        if (data.length > 0) {
          this.ps.er = this.ps.datas[0];
        } else {
          this.ps.er = [];
        }
      });

    }
  }

  close() {
    window.close();
  }

  editAirport($event: any) {
    this.airport = $event;

    this.ds.queryObject.airport = this.airport;
    this.fs.queryObject.airport = this.airport;
    this.ps.queryObject.airport = this.airport;
    this.is.queryObject.airport = this.airport;
    this.is.loadData(true, (data) => {
      if (data.length > 0) {
        this.is.er = this.is.datas[0];
      } else {
        this.is.er = [];
      }
    });

    this.ds.loadData(true, (data) => {
      if (data.length > 0) {
        this.ds.er = this.ds.datas[0];
      } else {
        this.ds.er = [];
      }
    });
    this.fs.loadData(true, (data) => {
      if (data.length > 0) {
        this.fs.er = this.fs.datas[0];
      } else {
        this.fs.er = [];
      }
    });
    this.ps.loadData(true, (data) => {
      if (data.length > 0) {
        this.ps.er = this.ps.datas[0];
      } else {
        this.ps.er = [];
      }
    });
  }

  editDate($event: any) {
    this.selectDate = this.dateFormat('YYYY-mm-dd', $event);

    this.ds.queryObject['createTime:>='] = this.selectDate;
    this.is.queryObject['createTime:>='] = this.selectDate;
    this.ps.queryObject['createTime:>='] = this.selectDate;
    this.fs.queryObject['createTime:>='] = this.selectDate;
    this.is.queryObject['createTime:<='] = this.selectDate;
    this.ds.queryObject['createTime:<='] = this.selectDate;
    this.fs.queryObject['createTime:<='] = this.selectDate;
    this.ps.queryObject['createTime:<='] = this.selectDate;

    this.is.loadData(true, (data) => {
      if (data.length > 0) {
        this.is.er = this.is.datas[0];
      } else {
        this.is.er = [];
      }
    });

    this.ds.loadData(true, (data) => {
      if (data.length > 0) {
        this.ds.er = this.ds.datas[0];
      } else {
        this.ds.er = [];
      }
    });
    this.fs.loadData(true, (data) => {
      if (data.length > 0) {
        this.fs.er = this.fs.datas[0];
      } else {
        this.fs.er = [];
      }
    });
    this.ps.loadData(true, (data) => {
      if (data.length > 0) {
        this.ps.er = this.ps.datas[0];
      } else {
        this.ps.er = [];
      }
    });
  }

  dateFormat(fmt, date) {
    let ret;
    const opt = {
      'Y+': date.getFullYear().toString(),        // 年
      'm+': (date.getMonth() + 1).toString(),     // 月
      'd+': date.getDate().toString(),            // 日
      'H+': date.getHours().toString(),           // 时
      'M+': date.getMinutes().toString(),         // 分
      'S+': date.getSeconds().toString()          // 秒
      // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (const k in opt) {
      ret = new RegExp('(' + k + ')').exec(fmt);
      if (ret) {
        fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')));
      }
    }
    return fmt;
  }
}
